﻿#include <QMap>
#include <QVariant>
#include "track_process.h"
#include "../data_modules/constant_variable.h"
#include "../data_modules/track_data.h"

Track_process::Track_process(QObject *parent)
    : QObject(parent)
{

}

Track_process::~Track_process()
{

}

void Track_process::sl_filter_track_data(QMap<QString, QVariant> *filter,
                                         QList<Track_data* >*list)
{
    if ( filter->isEmpty() && list->isEmpty()) {
        return;
    }

    // 读取用户选择
    QString flight_call_sign("");
    int ssr(0);
    int max_altitude(0);
    int min_altitude(0);

    QMap<QString, QVariant>::iterator map_iter = filter->begin();

    // Get user's filter
    while (filter->end() != map_iter) {
        if (0 == QString::compare(CALL_SIGN, map_iter.key())) {
            flight_call_sign = map_iter.value().toString();
        }

        if (0 == QString::compare(SSR_CODE, map_iter.key())) {
            ssr = map_iter.value().toInt();
        }

        if (0 == QString::compare(MAX_LATATIUDE, map_iter.key())) {
            max_altitude = map_iter.value().toInt();
        }

        if (0 == QString::compare(MIN_LATATIUDE, map_iter.key())) {
            min_altitude = map_iter.value().toInt();
        }

        ++map_iter;
    }

    QList<Track_data* >::iterator iter;
    int row_count(0);
    for (iter = list->begin(); list->end() != iter; ++iter) {
        QList<bool> show_track = QList<bool>();

        // Flight ssr for the track data
        int ssr_temp = (*iter)->get_data()->value(SSR_CODE).toInt();

        // User do not input ssr condition
        if (0 == ssr) {
            show_track.append(true);
        } else {
            // 输入SSR与航迹SSR相同，显示航迹
            if (ssr == ssr_temp) {
                show_track.append(true);
            } else {
                show_track.append(false);
            }
        }

        // 航迹高度，根据高度为“h0”或“h”分别处理
        int altitude_temp(0);
        if ((*iter)->get_data()->contains(H0)) {
            altitude_temp = (*iter)->get_data()->value(H0, 0).toInt();
        }

        if ((*iter)->get_data()->contains("h")) {
            altitude_temp = (*iter)->get_data()->value("h", 0).toInt();
        }

        if ((altitude_temp < max_altitude) && (altitude_temp > min_altitude)) {
            show_track.append(true);
        } else {
            show_track.append(false);
        }

        QString flight_temp = (*iter)->get_data()->value(CALL_SIGN).toString();

        // User do not input call sign condition
        if (0 == QString::compare("", flight_call_sign)) {
            show_track.append(true);
        } else {
            if (0 == QString::compare(flight_temp, flight_call_sign)) {
                show_track.append(true);
            } else {
                show_track.append(false);
            }
        }

        QList<bool>::iterator bool_iter = show_track.begin();
        bool flag(true);
        while (show_track.end() != bool_iter) {
            flag &= (*bool_iter);
            ++bool_iter;
        }

        // Reset track_data indicator for showing
        (*iter)->set_show_track(flag);
        emit si_update_track_by_filter(row_count, (*iter)->get_show_track());
        ++row_count;
    }
}
